package cn.otrue.patienthealthmanager.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.adapter.CenterMessageAdapter;
import cn.otrue.patienthealthmanager.bean.MessageCenterModel;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.MyConst;
import cn.otrue.patienthealthmanager.view.XListView;
import cn.otrue.patienthealthmanager.view.XListView.IXListViewListener;

import com.google.gson.reflect.TypeToken;

/**
 * 患者回复医生关注
 */
public class MessagePHFDGZragment extends BaseFragment implements
		IXListViewListener {
	private View view;
	private XListView lVi_center_message;
	private CenterMessageAdapter adapter;
	private List<MessageCenterModel> mapMessageData;
	private int currentPage = 1;

	@Override
	public void onClick(View arg0) {
	}

	@Override
	public String setPageName() {
		// TODO Auto-generated method stub
		return null;
	}

	public static Fragment newInstance() {
		return new MessagePHFDGZragment();
	}

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_patient_message_all,
				container, false);
		mapMessageData = new ArrayList<MessageCenterModel>();
		return view;
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initView();
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (isVisibleToUser) {
			// 相当于onrsume
			if (mapMessageData != null) {
				mapMessageData.clear();
			}
			currentPage = 1;
			getData(currentPage);
		}
	}

	public void initView() {
		lVi_center_message = (XListView) view
				.findViewById(R.id.lV_pat_center_message);
		lVi_center_message.setPullLoadEnable(true); // 是否开启上拉加载
		lVi_center_message.setXListViewListener(this);// 上啦加载和下拉刷新的监听
		lVi_center_message.setPullRefreshEnable(false);
	}

	// 获取数据
	public void getData(int i) {

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("sender", MyConst.getACCOUNT());
		map.put("typeid", MyConst.TYPEID_D_respond_P);
		TransferParameter parameter = new TransferParameter();
		// parameter.setUsername(MyConst.ACCOUNT);
		parameter.setPagesize("20");
		parameter.setCurrentPage(i + "");
		parameter.setData(map);
		new HttpServer().post(HttpCons.QUERY_DOCTOR_MESSAGE, parameter,
				messageHandler);
	}

	private boolean isData;
	private Handler messageHandler = new ServerHandler() {

		@Override
		public void onSuccess(Bundle data, String state, String msg) {

			String dataStr = data.getString(HttpCons.DATA);
			List<MessageCenterModel> newList = new ArrayList<MessageCenterModel>();
			if ("001".equals(state)) {

				if ("".equals(dataStr)) {
					if (isData == false) {
						Toast.makeText(getActivity(), "您还没有回复医生关注",
								Toast.LENGTH_SHORT).show();
					}
				} else {
					newList = HttpServer.gson.fromJson(dataStr,
							new TypeToken<List<MessageCenterModel>>() {
							}.getType());
					mapMessageData.addAll(newList);
					if (mapMessageData.size() >= 5) {
						lVi_center_message.setPullLoadEnable(true);
					}
					if (adapter == null) {
						adapter = new CenterMessageAdapter(mapMessageData,
								getActivity(), adapterHandler);
						lVi_center_message.setAdapter(adapter);
					} else {
						adapter.notifyDataSetChanged();
					}
					isData = true;
				}
			} else {
				if (newList.size() == 0 && isData == false) {
					Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT)
							.show();
				}
			}
			onLoad();
		}

		@Override
		public void onFail(Bundle data, String state) {
			// TODO Auto-generated method stub
			super.onFail(data, state);
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(getActivity(), "网络异常,请稍后重试", Toast.LENGTH_SHORT)
						.show();
			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
			onLoad();
		}

	};
	/*
	 * 为了同意或查看后数据的重新加载
	 */
	private Handler adapterHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			currentPage = 1;
			getData(currentPage);
		};
	};

	private void onLoad() {
		lVi_center_message.stopRefresh();
		lVi_center_message.stopLoadMore();
		lVi_center_message.setRefreshTime("刚刚");
	}

	@Override
	public void onRefresh() {
		mapMessageData.clear();
		currentPage = 1;
		getData(currentPage);

	}

	@Override
	public void onLoadMore() {
		currentPage++;
		getData(currentPage);
	}
}
